﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="ObligorData", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(ObligorData))]
    public class ObligorDataSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IObligorData clazz;
        
        public ObligorDataSerializer(IObligorData clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("Obligor");
            if (clazz.Obligor!=null) {
                global::Xtee.Core.SerializationMetadata metaObligor = this.GetMetaData(clazz.Obligor);
                Writer.WriteTypeAttribute(metaObligor.TypeQualifiedName.Namespace, metaObligor.TypeQualifiedName.Name);
                metaObligor.NewSerializer(clazz.Obligor).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Obligor");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("ObligorDescription");
            if (clazz.ObligorDescription!=null) {
                global::Xtee.Core.SerializationMetadata metaObligorDescription = this.GetMetaData(clazz.ObligorDescription);
                Writer.WriteTypeAttribute(metaObligorDescription.TypeQualifiedName.Namespace, metaObligorDescription.TypeQualifiedName.Name);
                metaObligorDescription.NewSerializer(clazz.ObligorDescription).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ObligorDescription");
            }
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.ObligorRepresentativeData != null) 
                        && (x < clazz.ObligorRepresentativeData.Count)); x = (x + 1)) {
                if (clazz.ObligorRepresentativeData[x]!=null) {
                    Writer.WriteStartElement("ObligorRepresentativeData");
                    global::Xtee.Core.SerializationMetadata metaObligorRepresentativeData = this.GetMetaData(clazz.ObligorRepresentativeData[x]);
                    Writer.WriteTypeAttribute(metaObligorRepresentativeData.TypeQualifiedName.Namespace, metaObligorRepresentativeData.TypeQualifiedName.Name);
                    metaObligorRepresentativeData.NewSerializer(clazz.ObligorRepresentativeData[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (((clazz.Address == null) 
                        || (clazz.Address.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "Address");
            }
            for (int x = 0; ((clazz.Address != null) 
                        && (x < clazz.Address.Count)); x = (x + 1)) {
                Writer.WriteStartElement("Address");
                if (clazz.Address[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaAddress = this.GetMetaData(clazz.Address[x]);
                    Writer.WriteTypeAttribute(metaAddress.TypeQualifiedName.Namespace, metaAddress.TypeQualifiedName.Name);
                    metaAddress.NewSerializer(clazz.Address[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Address");
                }
                Writer.WriteEndElement();
            }
            for (int x = 0; ((clazz.ContactInformation != null) 
                        && (x < clazz.ContactInformation.Count)); x = (x + 1)) {
                if (clazz.ContactInformation[x]!=null) {
                    Writer.WriteStartElement("ContactInformation");
                    global::Xtee.Core.SerializationMetadata metaContactInformation = this.GetMetaData(clazz.ContactInformation[x]);
                    Writer.WriteTypeAttribute(metaContactInformation.TypeQualifiedName.Namespace, metaContactInformation.TypeQualifiedName.Name);
                    metaContactInformation.NewSerializer(clazz.ContactInformation[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="Obligor") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ObligorData>Obligor");
                }
                global::Xtee.Core.SerializationMetadata metaObligor = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligor>(Reader, "Obligor", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.Obligor = metaObligor.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligor>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("Obligor");
                    metaObligor.NewSerializer(clazz.Obligor).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ObligorData>Obligor", Reader.LocalName);
            }
            if (Reader.LocalName=="ObligorDescription") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ObligorData>ObligorDescription");
                }
                global::Xtee.Core.SerializationMetadata metaObligorDescription = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorDescription>(Reader, "ObligorDescription", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.ObligorDescription = metaObligorDescription.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorDescription>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ObligorDescription");
                    metaObligorDescription.NewSerializer(clazz.ObligorDescription).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ObligorData>ObligorDescription", Reader.LocalName);
            }
            if (Reader.LocalName=="ObligorRepresentativeData") {
                clazz.ObligorRepresentativeData = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorRepresentativeData>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "ObligorRepresentativeData")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ObligorData>ObligorRepresentativeData");
                    }
                    global::Xtee.Core.SerializationMetadata metaObligorRepresentativeData = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorRepresentativeData>(Reader, "ObligorRepresentativeData", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorRepresentativeData item = metaObligorRepresentativeData.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorRepresentativeData>();
                    clazz.ObligorRepresentativeData.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("ObligorRepresentativeData");
                        metaObligorRepresentativeData.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            clazz.Address = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "Address")); ) {
                if (Reader.LocalName=="Address") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ObligorData>Address");
                    }
                    global::Xtee.Core.SerializationMetadata metaAddress = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>(Reader, "Address", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress item = metaAddress.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>();
                    clazz.Address.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Address");
                        metaAddress.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ObligorData>Address", Reader.LocalName);
                }
            }
            if ((clazz.Address.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "Address");
            }
            if (Reader.LocalName=="ContactInformation") {
                clazz.ContactInformation = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "ContactInformation")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ObligorData>ContactInformation");
                    }
                    global::Xtee.Core.SerializationMetadata metaContactInformation = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>(Reader, "ContactInformation", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation item = metaContactInformation.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>();
                    clazz.ContactInformation.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("ContactInformation");
                        metaContactInformation.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
        }
    }
}
